write to file
Type
command
Summary
Places data in a file that has been opened with the open file command.
Syntax
write <value> to {file <pathName> | stdout} [at {<start> | EOF | end}]
Description
Use the write to file command to change the contents of a file.
If the file was opened in write mode, the write to file command completely replaces the file contents from the start. For example, if the file originally contains "ABC", and you write "1" to it, after the write to file command is executed the file contains "1".
If the file was opened in update mode, if you write less data to the file than it already contains, the write to file command does not remove characters from it. For example, if the file originally contains "ABC", and you write "1" to it, after the write to file command is executed the file contains "1BC".
If the file was opened in append mode, the write begins at the end of the file.
After writing, you must close the file with the close file command.
The write to stdout form writes to the standard output (on Unix systems). The standard output is always open, so you can write to it without first opening it.
As an alternative to the open file and write to file commands, you can also use the URL keyword with the put command to change the contents of a file.
Parameters
Name | Type | Description |
---|---|---|
value | string | The data to be written to the file. |
pathName | The pathName specifies the name and location of the file you want to write to. It must be the same as the path you used with the open file command. The pathName is case-sensitive, even on platforms where file names are not case-sensitive. If you specify the name of a serial port on Mac OS or Windows systems, LiveCode writes to the specified port. The names of serial ports end in a colon (:). | |
start | enum | The start specifies the character or byte position in the file where you want to begin writing. A positive number begins start characters after the beginning of the file; a negative number begins start characters before the end of the file. If you specify either of the synonyms EOF or end, the write begins after the last character in the file. If you don't specify a start, the write begins:
|
Examples
write "test" to file "test.txt"
write linefeed to stdout
write "Hello" & return to stdout
write "ATZ" to file "modem:"
mouseUp
local tFile
put specialFolderPath("desktop") & "/test.txt" into tFile
open file tFile for text write
write "one 222" to file tFile -- Writes to the start of the file
write "two" to file tFile at 4 -- Writes to the file from character 4
write " three" to file tFile at EOF -- Writes to the end of the file
write " four" to file tFile at end -- Writes to the end of the file
close file tFile
mouseUp
Related
command: open file, close file, write to driver, put
function: result
keyword: file, characters, stdout, URL
Compatibility and Support
Introduced
LiveCode 1.0
OS
mac
windows
linux
ios
android
Platforms
desktop
server
mobile